package bidsanity



import grails.test.mixin.*
import org.junit.*

/**
 * Unit tests for the CustomerController class.
 */
@TestFor(CustomerController)
@Mock([Customer, Bid, Listing])
class CustomerControllerTests {

    void testCreate() {
        def admin = new Customer(emailAddress: 'admin@example.org', password: "testing", isAdmin: true).save(validate: true)
        controller.session.user = admin.emailAddress
        def customer = controller.create()
        Assert.assertNotNull(customer)
    }
    
    void testSave() {
        controller.customerService = new CustomerService()
        Map myParams = [emailAddress: 'seller@example.org', password: "testing"]
        controller.params.putAll(myParams)
        controller.save()
        Assert.assertEquals(Customer.count(), 1)
        Assert.assertNotNull(Customer.list()[0].id)
        Assert.assertTrue(response.redirectedUrl == "/customer/login/${Customer.list()[0].id}")
    }

    void testSaveInvalid() {
        controller.customerService = new CustomerService()
        Map myParams = [emailAddress: 'seller@', password: "testing"]
        controller.params.putAll(myParams)
        controller.save()
        Assert.assertEquals(Customer.count(), 0)
        Assert.assertTrue(view == "/customer/create")
        Assert.assertNotNull(model.customerInstance)
        Assert.assertEquals(flash.message, "default.not.created.message")
    }

    void testIndex(){
        Customer c = new Customer()
        c.id = 1
        c.save(validate: false)
        def controllerResults = controller.index()
        Assert.assertEquals(1, controllerResults.customerInstanceTotal)
    }


    void testShow() {
        Customer c = new Customer()
        c.id = 1
        c.save(validate: false)
        Assert.assertEquals(1, Customer.count)


        Map myParams = [id: c.id]
        controller.params.putAll(myParams)
        def customer = controller.show()
        Assert.assertNotNull(customer)
        
    }

    void testShowNoCustomer()
    {
        Map myParams = [id: 1]
        controller.params.putAll(myParams)
        def customer = controller.show()
        Assert.assertNull(customer)
        Assert.assertTrue(response.redirectedUrl == '/customer/index')

    }

    void testDeleteNoBids() {
        def cust = new Customer(emailAddress: 'seller@example.org', password: "testing").save(validate: true)
        controller.session.user = cust.emailAddress
        Map myParams = [id:cust.id]
        controller.params.putAll(myParams)
        controller.delete()

        Assert.assertTrue(flash.message == 'default.deleted.message')
        Assert.assertTrue(response.redirectedUrl == '/customer/index')
    }
//    moved to integration test
//    void testDeleteWithListing() {
//        def admin = new Customer(emailAddress: 'admin@example.org', password: "testing", isAdmin: true).save(validate: true, flush: true)
//        controller.session.user = admin.emailAddress
//        def s = new Customer(emailAddress: 'seller@example.org', password: "testing").save(validate: true, flush: true)
//        def l = new Listing(
//                name: 'TestListing',
//                endDate: new Date() + 10,
//                startingBidPrice: 1.0D,
//                description: 'a test listing',
//                seller: s).save(validate: true, flush: true)
//        s.save(validate: true, flush: true)
//        Assert.assertTrue(Listing.findAllBySeller(s).size() > 0)
//        Map myParams = [id:s.id]
//        controller.params.putAll(myParams)
//        controller.delete()
//        println(flash.message)
//        Assert.assertTrue(flash.message == 'customer.has.listing')
//        Assert.assertTrue(response.redirectedUrl == "/customer/show/${s.id}")
//    }

    void testDeleteWithBid(){
        def s = new Customer(emailAddress: 'seller@example.org', password: "testing").save(validate: true)
        def bidder = new Customer(emailAddress: 'bidder@example.org', password: "testing").save(validate: true)
        controller.session.user = bidder.emailAddress
        def l = new Listing(
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing',
                seller: s).save(validate: true)
        new Bid(bidder: bidder, listing:  l, amount: 12.0D).save()

        Map myParams = [id:bidder.id]
        controller.params.putAll(myParams)
        controller.delete()

        Assert.assertTrue(flash.message == 'customer.bids.not.empty')
        Assert.assertTrue(response.redirectedUrl == "/customer/show/${bidder.id}")
    }
    
}
